Page({
  data: {
    menuItems: [],
    showModal: false,
    isEdit: false,
    categories: ['热销', '新品', '套餐', '饮品', '甜点'],
    formData: {
      id: '',
      name: '',
      price: '',
      categoryIndex: 0,
      image: ''
    }
  },

  onLoad: function() {
    // 从本地存储加载菜品数据
    const menuItems = wx.getStorageSync('menuItems') || []
    this.setData({ menuItems })
  },

  // 显示添加菜品弹窗
  showAddModal: function() {
    console.log('显示添加弹窗')
    this.setData({
      showModal: true,
      isEdit: false,
      formData: {
        id: '',
        name: '',
        price: '',
        categoryIndex: 0,
        image: ''
      }
    })
  },

  // 显示编辑菜品弹窗
  onEdit: function(e) {
    const id = e.currentTarget.dataset.id
    const item = this.data.menuItems.find(item => item.id === id)
    if (item) {
      this.setData({
        showModal: true,
        isEdit: true,
        formData: {
          id: item.id,
          name: item.name,
          price: item.price,
          categoryIndex: this.data.categories.indexOf(item.category),
          image: item.image
        }
      })
    }
  },

  // 隐藏弹窗
  hideModal: function() {
    this.setData({ showModal: false })
  },

  // 输入框事件处理
  onNameInput: function(e) {
    this.setData({
      'formData.name': e.detail.value
    })
  },

  onPriceInput: function(e) {
    this.setData({
      'formData.price': e.detail.value
    })
  },

  onCategoryChange: function(e) {
    this.setData({
      'formData.categoryIndex': e.detail.value
    })
  },

  // 选择图片
  chooseImage: function() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          'formData.image': res.tempFilePaths[0]
        })
      }
    })
  },

  // 提交表单
  submitForm: function() {
    const { name, price, categoryIndex, image } = this.data.formData
    if (!name || !price || !image) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }

    const newItem = {
      id: this.data.isEdit ? this.data.formData.id : Date.now().toString(),
      name,
      price: parseFloat(price),
      category: this.data.categories[categoryIndex],
      image
    }

    let menuItems = [...this.data.menuItems]
    if (this.data.isEdit) {
      const index = menuItems.findIndex(item => item.id === newItem.id)
      menuItems[index] = newItem
    } else {
      menuItems.push(newItem)
    }

    this.setData({ menuItems })
    wx.setStorageSync('menuItems', menuItems)
    this.hideModal()

    wx.showToast({
      title: this.data.isEdit ? '修改成功' : '添加成功',
      icon: 'success'
    })
  },

  // 删除菜品
  onDelete: function(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个菜品吗？',
      success: (res) => {
        if (res.confirm) {
          const menuItems = this.data.menuItems.filter(item => item.id !== id)
          this.setData({ menuItems })
          wx.setStorageSync('menuItems', menuItems)
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          })
        }
      }
    })
  }
}) 